home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / GraphicsWorkshop / Source / Examples / ToTiff / ControlLoader.m < prev    next >
Encoding:
Text File  |  1991-10-20  |  2.4 KB  |  105 lines

  1. #import <stdio.h>
  2. #import <stdlib.h>
  3. #import <strings.h>
  4. #import <defaults.h>
  5. #import <sys/types.h>
  6. #import <sys/dir.h>
  7. #import <sys/param.h>
  8. #import <objc/objc-load.h>
  9. #import <appkit/Application.h>
  10. #import "ControlLoader.h"
  11.  
  12. @implementation ControlLoader
  13.  
  14.     id            controlObject;
  15.  
  16. - (char *)getImageControlFromPath: (char *)path ofType: (const char *)type
  17. {
  18.     DIR            *myDir;
  19.     struct direct    *myEntry;
  20.     static char    buffer[MAXPATHLEN];
  21.     char            typeBuffer[MAXPATHLEN];
  22.     
  23.     sprintf(typeBuffer, "%s.controls", type);
  24.     fprintf(stderr, "Opening %s to find %s\n", path, typeBuffer);
  25.     myDir = opendir(path);
  26.     if (myDir) {
  27.         while (myEntry = readdir(myDir)) {
  28.             if (!strcmp(myEntry->d_name, typeBuffer)) {
  29.                 strcpy(buffer, path);
  30.                 strcat(buffer, myEntry->d_name);
  31.                 fprintf(stderr, "Found image tools: %s\n", buffer);
  32.                 break;
  33.             }
  34.         }
  35.         closedir(myDir);
  36.     }
  37.     else {
  38.         fprintf(stderr, "Unable to open dir\n");
  39.         return NULL;
  40.     }
  41.  
  42.     return buffer;
  43. }
  44.  
  45. - (char *)getImageControlsOfType: (const char *)type
  46. {
  47.     char        buffer[1000];
  48.     char        *imagePath = NULL;
  49.     char        *tmpPath;
  50.     
  51.     if (tmpPath=[self getImageControlFromPath: "/NextLibrary/Converters/" ofType: type]) {
  52.         imagePath = tmpPath;
  53.     }
  54.     if (tmpPath=[self getImageControlFromPath: "/LocalLibrary/Converters/" ofType: type]) {
  55.         imagePath = tmpPath;
  56.     }
  57.     sprintf(buffer, "%s/Library/Converters/", NXHomeDirectory());
  58.     if (tmpPath = [self getImageControlFromPath: buffer ofType: type]) {
  59.         imagePath = tmpPath;
  60.     }
  61.  
  62.     return imagePath;
  63. }
  64.  
  65. void LinkImageControlsCallBack(Class cl, Category ca)
  66. {
  67.     fprintf(stderr, "Call back for control object reached\n");
  68.     controlObject = (id)cl;
  69.     fprintf(stderr, "Linked object at %p\n", cl);
  70. }
  71.  
  72. + loadControl: (const char *)type
  73. {
  74.     NXStream    *myStream;
  75.     char            *cvtFiles[] = {    NULL,
  76.                                 "/usr/lib/libcs.a",
  77.                                 "/usr/lib/libm.a", 
  78.                                 "/usr/lib/libNeXT_s.a",
  79.                                 "/lib/libsys_s.a",
  80.                                 NULL };
  81.     
  82.     self = [super new];
  83.  
  84.     cvtFiles[0] = [self getImageControlsOfType: type];
  85.     if (!cvtFiles[0]) {
  86.         fprintf(stderr, "Fatal Error, unable to open controls type: %s\n", type);
  87.         [self free];
  88.         return nil;
  89.     }
  90.     myStream = NXOpenFile(fileno(stderr), NX_WRITEONLY);
  91.     fprintf(stderr, "stderr linked to stream myStream\n");
  92.     if (objc_loadModules(cvtFiles, myStream, LinkImageControlsCallBack, NULL, NULL)) {
  93.         NXFlush(myStream);
  94.         return nil;
  95.     }
  96.     NXFlush(myStream);
  97.     NXClose(myStream);
  98.  
  99.     [self free];
  100.     
  101.     return controlObject;
  102. }
  103.  
  104. @end
  105.